<?php

class DeveloperController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	//public $layout='//layouts/column2';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return $this->getRules();
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionIndex(){
            //init page value
            $pagevals = array(
                'page_title' => "Quản trị nhà phát triển",
                'subpage_title' => 'Danh sách nhà phát triển',
                //'action' => 'developer/abc'
            );
            
            //check input params and validate input
            $config = ConfigModel::model()->findByPk('1')->attributes;
            $paging_limit_number = $config['paging_limit_number'];
            $status = isset($_GET['status']) ? $_GET['status'] : 'active';
            $search_string = isset($_GET['search_string']) ? $_GET['search_string'] : '';
            $page_number = isset($_GET['page_number']) ? intval($_GET['page_number']) : 1;
            $vote = isset($_GET['vote']) ? $_GET['vote'] : 'all';
            
            $condition = array();
            $sort = array('interested_point'=>'desc','create_time'=>'desc');
            
            //validate input
            if($page_number <= 0 || is_numeric($page_number)!= true )$page_number=1;
            if($status != 'all'){ $condition['status'] = $status; }
            if($vote != 'all'){$condition['vote'] = $vote;}
            
            if($search_string == null || $search_string == ""){
                $data = DeveloperModel::model()->getByPage($condition, $page_number, $paging_limit_number, $sort);
                $pagevals['total_row'] = DeveloperModel::model()->countRow($condition);
            }else{
                $fields = array('name','homepage','slug','address','total_game','interested_point','vote');
                $data = DeveloperModel::model()->SearchMultiField($search_string, $condition, $page_number, $paging_limit_number, $fields);
                $pagevals['total_row'] = DeveloperModel::model()->countSearch($search_string, $condition, $fields);
            }
            
            $pagevals['page_number'] = $page_number;
            $pagevals['total_page'] = DeveloperModel::model()->countPage($pagevals['total_row'], $paging_limit_number);
            $pagevals['row_start'] = DeveloperModel::model()->get_start_row($pagevals['total_row'], $page_number, $paging_limit_number);
            $pagevals['row_end'] = DeveloperModel::model()->get_end_row($pagevals['total_row'], $page_number, $paging_limit_number);
            $pagevals['status'] = $status;
            $pagevals['search_string'] = $search_string;
            $pagevals['vote']= $vote;

            $this->display("developer/index", array('data'=>$data, 'pagevals'=>$pagevals) );
        }
        
        public function actionCreate()
	{
            $pagevals = array(
                'page_title' => "Quản trị nhà phát triển",
                'subpage_title' => 'Tạo nhà phát triển mới',
                //'action' => 'developer/abc'
            );
            
            ///--------------------------
            $time=time();
            $developer = new DeveloperModel;
            if(isset($_POST['name'])){
                if(!isset($_POST['total_game']) || !is_numeric($_POST['total_game'])){ $_POST['total_game']= 0;}
                if(!isset($_POST['vote']) || !is_numeric($_POST['vote'])){ $_POST['vote']= 0;}
                if(!isset($_POST['interested_point']) || !is_numeric($_POST['interested_point'])){ $_POST['interested_point']= 0;}
  
                $developer->attributes = array(
                                'name' => $_POST['name'],
                                'slug' => $_POST['slug'],
                                'homepage' => $_POST['homepage'],
                                'short_intro'=>$_POST['short_intro'],
                                'logo' => $_POST['logo'],
                                'intro' => $_POST['intro'],
                                'address' => $_POST['address'],
                                'total_game' => intval($_POST['total_game']),
                                'interested_point' => intval($_POST['interested_point']),
                                'vote' => intval($_POST['vote']),
                                'status' => $_POST['status'],
                                'create_time' => $time,
                                'update_time' =>  $time,
                            );
                if($developer->save(false)){
                    $this->redirect(array('index','status'=>$_POST['status']));
                }
            }
            $this->display("developer/create", array('pagevals'=>$pagevals));
	}
        

        public function actionEdit($id)
	{
            if(!isset($_GET['id'])){
                die('Developers not found');
            }
                
            $pagevals = array(
                'page_title' => "Quản trị nhà phát triển",
                'subpage_title' => 'Chỉnh sửa nhà phát triển',
                //'action' => 'developer/abc'
            );
            //-------------------------------
            //get developer data
            $developer = DeveloperModel::model()->findByPk($id);
            if(!is_object($developer)){
                die('Developers not found');
            }
            ///--------------------------
            $time=time();
            
            if(isset($_POST['name'])){
                if(!isset($_POST['total_game']) || !is_numeric($_POST['total_game'])){ $_POST['total_game']= 0;}
                if(!isset($_POST['vote']) || !is_numeric($_POST['vote'])){ $_POST['vote']= 0;}
                if(!isset($_POST['interested_point']) || !is_numeric($_POST['interested_point'])){ $_POST['interested_point']= 0;}
  
                $developer->attributes = array(
                                'name' => $_POST['name'],
                                'slug' => $_POST['slug'],
                                'homepage' => $_POST['homepage'],
                                'logo' => $_POST['logo'],
                                'short_intro'=>$_POST['short_intro'],
                                'intro' => $_POST['intro'],
                                'address' => $_POST['address'],
                                'total_game' => intval($_POST['total_game']),
                                'interested_point' => intval($_POST['interested_point']),
                                'vote' => intval($_POST['vote']),
                                'status' => $_POST['status'],
                                'create_time' => $time,
                                'update_time' =>  $time,
                    );
                if($developer->save(false)){
                    $this->redirect(array('index','status'=>$_POST['status']));
                }
            }
            $this->display("developer/edit", array('pagevals'=>$pagevals,'data'=>$developer->attributes));
        }
        
        
        
        
        
        public function doAcion($status,$data){
            $i = 0;
            $time = time();
            $developer_id = explode(';', $data);
            if(sizeof($developer_id) == 0){
                echo 0;
            }
            else{
                foreach($developer_id as $value){
                    if($value != 0){
                        $result = DeveloperModel::model()->modify($value,array('status'=>$status,'update_time'=>$time));
                        if($result == true){
                            $i++;
                        }
                    }
                }
                echo $i;
            }
        }
        
        
        public function actionCopy(){
            if(isset($_POST['developer_id'])){
                $i = 0;
                $developer_id = explode(';', $_POST['developer_id']);
                if(sizeof($developer_id) == 0){
                    echo 0;
                }
                else{
                    foreach($developer_id as $value){
                        if($value != 0){
                            $result = DeveloperModel::model()->copy($value);
                            if($result == true){
                                $i++;
                            }
                        }
                    }
                    echo $i;
                }
            }
        }
        
        public function actionRemove(){
            if(isset($_POST['remove_ids'])){
                $this->doAcion('deleted', $_POST['remove_ids']);
            }
            if(isset($_POST['id'])){
                $result = DeveloperModel::model()->modify($_POST['id'],array('status'=>'deleted','update_time'=>time()));
                if($result == true) echo 'success';
            }
            
        }
        
        public function actionPending(){
            if(isset($_POST['pending_ids'])){
                $this->doAcion('pending', $_POST['pending_ids']);
            }
            if(isset($_POST['id'])){
                $result = DeveloperModel::model()->modify($_POST['id'],array('status'=>'pending','update_time'=>time()));
                if($result == true) echo 'success';
            }
            
        }
        
        public function actionApprove(){
            if(isset($_POST['approve_ids'])){
                $this->doAcion('approved', $_POST['approve_ids']);
            }
            if(isset($_POST['id'])){
                $result = DeveloperModel::model()->modify($_POST['id'],array('status'=>'approved','update_time'=>time()));
                if($result == true) echo 'success';
            }
            
        }
        
        public function actionActive(){
            if(isset($_POST['active_ids'])){
                $this->doAcion('active', $_POST['active_ids']);
            }
            if(isset($_POST['id'])){
                $result = DeveloperModel::model()->modify($_POST['id'],array('status'=>'active','update_time'=>time()));
                if($result == true) echo 'success';
            }
            
        }
        
        public function actionBlock(){
            if(isset($_POST['block_ids'])){
                $this->doAcion('blocked', $_POST['block_ids']);
            }
            
            if(isset($_POST['id'])){
                $result = DeveloperModel::model()->modify($_POST['id'],array('status'=>'blocked','update_time'=>time()));
                if($result == true) echo 'success';
            }
            
        }
        
        public function actionRestore(){
            if(isset($_POST['restore_ids'])){
                $this->doAcion('pending', $_POST['restore_ids']);
            }
            if(isset($_POST['id'])){
                $result = DeveloperModel::model()->modify($_POST['id'],array('status'=>'pending','update_time'=>time()));
                if($result == true) echo 'success';
            }
            
        }
        
        public function getFilterValue($fields){
            if(is_array($fields)){
                $result = array();
                foreach($fields as $key => $value){
                    $temp = DeveloperModel::model()->getUnique($key, $value);
                    if(is_array($temp)){
                        foreach($temp as $val){
                            if($val[$key] !=null && $val[$key]!=''){
                                $result[$key][]= $val[$key];
                            }
                        }
                    }
                }
                return $result;
            }else return null;
        }

	/**
	 * Performs the AJAX validation.
	 * @param DeveloperModel $model the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='developer-model-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
